﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Collections.Generic;


public partial class Manage_Cms_systemSubjectItem : System.Web.UI.Page
{
    protected ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    //获取请求过来的信息
    protected KeRui.BLL.SysConfig bllSysConfig = new KeRui.BLL.SysConfig();
    protected KeRui.Model.SysConfig modelSysConfig;
    protected int TemplateID;
    protected int FileType;


    protected KeRui.BLL.CmsTemplateFile bllCmsTemplateFile = new KeRui.BLL.CmsTemplateFile();
    protected KeRui.Model.CmsTemplateFile modelCmsTemplateFile = new KeRui.Model.CmsTemplateFile();

    private KeRui.BLL.CmsTemplateFileBackUp bllCmsTemplateFileBackUp = new KeRui.BLL.CmsTemplateFileBackUp();
    private KeRui.Model.CmsTemplateFileBackUp modelCmsTemplateFileBackUp = new KeRui.Model.CmsTemplateFileBackUp();



    

    protected void Page_Load(object sender, EventArgs e)
    {
        ManageHelper.CheckAdminLogin();//登陆判断 
        TemplateID = PageRequest.GetInt("TemplateID", -1);//
        FileType = PageRequest.GetInt("FileType", -1);
        modelCmsTemplateFile.FileType = FileType;
        modelSysConfig = bllSysConfig.GetModel("TemplateFileType", FileType.ToString());
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            case "reduce":
                Reduce();
                break;
            case "view":
                View();
                break;
            default:
                if (modelSysConfig == null)
                {
                    nsb.AddErr("Error。"); nsb.ChkErr();
                }
                break;
        }

    }

    #region 添加
    private void Add()
    {
        ManageHelper.CheckAdminPower("system_addsubject");//权限
        modelCmsTemplateFile.FileName = PageRequest.GetString("FileName");
        if (modelCmsTemplateFile.FileName == "")
        {
            nsb.AddErr("请输入文件名称。");
        }
        modelCmsTemplateFile.FileUpdateInterval = PageRequest.GetInt("FileUpdateInterval", -1);
        if (modelCmsTemplateFile.FileUpdateInterval == -1)
        {
            nsb.AddErr("请输入文件自动更新间隔。");
            try
            {
                Convert.ToInt32(modelCmsTemplateFile.FileUpdateInterval);
            }
            catch
            {
                nsb.AddErr("文件自动更新间隔应该为一个整数。");
            }
        }
        modelCmsTemplateFile.FileContent = PageRequest.GetString("FileContent");
        if (modelCmsTemplateFile.FileContent == "")
        {
            nsb.AddErr("请输入文件内容。");
        }
        modelCmsTemplateFile.TemplateID = PageRequest.GetInt("TemplateID", -1);
        if (modelCmsTemplateFile.TemplateID == -1)
        {
            nsb.AddErr("Error。所属模板。");
        }
        modelCmsTemplateFile.FileType = PageRequest.GetInt("FileType", -1);
        if (modelCmsTemplateFile.FileType == -1)
        {
            nsb.AddErr("Error。文件类型。");
        }

        modelCmsTemplateFile.IsDefault = 1;
        modelCmsTemplateFile.IsTop = PageRequest.GetInt("IsTop", 0);
        nsb.ChkErr();
        //写入文件　模板
        KeRui.BLL.CmsTemplate bllCmsTemplate = new KeRui.BLL.CmsTemplate();
        KeRui.Model.CmsTemplate modelCmsTemplate = bllCmsTemplate.GetModel(modelCmsTemplateFile.TemplateID);
        FileObj.WriteFileNoSign(modelCmsTemplate.TemplateDir + "/" + modelCmsTemplate.TemplateFolder + "/" + modelCmsTemplateFile.FileName + ".shtml", modelCmsTemplateFile.FileContent);
        int newfileid = bllCmsTemplateFile.Add(modelCmsTemplateFile);
        if (newfileid < 0)
        {
            nsb.AddErr("已存在相同的模板文件。");
            nsb.ChkErr();
        }
        else
        {
            if (modelCmsTemplateFile.FileType == 7)
            {
                KeRui.BLL.CmsTemplateFileAttr BLLCmsTemplateFileAttr = new KeRui.BLL.CmsTemplateFileAttr();
                KeRui.Model.CmsTemplateFileAttr ModelCmsTemplateFileAttr = new KeRui.Model.CmsTemplateFileAttr();
                ModelCmsTemplateFileAttr.FileID = newfileid;
                ModelCmsTemplateFileAttr.HtmlPath = "";
                ModelCmsTemplateFileAttr.PicPath = "";
                ModelCmsTemplateFileAttr.Name = modelCmsTemplateFile.FileName;
                BLLCmsTemplateFileAttr.Add(ModelCmsTemplateFileAttr);
            }
            ManageHelper.AddLog("system_addsubject" , "添加了“" + modelCmsTemplateFile.FileName + "”模板文件。");
            Response.Write("<script type=\"text/javascript\">window.parent.location.href='systemSubjectList.aspx?&sucmsg=恭喜。添加成功。'</script>");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int FileID = PageRequest.GetInt("ID", -1);
        if (FileID == -1)
        {
            nsb.AddErr("该模板文件不存在。");
            nsb.ChkErr();
        }
        modelCmsTemplateFile = bllCmsTemplateFile.GetModel(FileID);
        //Response.Write(modelCmsTemplateFile.FileContent); Response.End();
        string strTpl = KeRui.BLL.Common.LoadTxtFile(modelCmsTemplateFile.FilePath, 1);
        int state = PageRequest.GetInt("state", -1);
        if (state == 1)
        {
            modelCmsTemplateFile.FileContent = strTpl;
        }
        else if (state == 0)
        {

        }
        else
        {
            if (strTpl != modelCmsTemplateFile.FileContent)
            {
                Response.Write("<script type=\"text/javascript\">if(confirm(\"模板文件已被更改。是否获取模板内容？\")){location.href=\"systemSubjectItem.aspx?&act=edit&FileType=" + modelCmsTemplateFile.FileType + "&ID=" + modelCmsTemplateFile.FileID + "&state=1\"}" +
                    "else" +
                    "{location.href=\"systemSubjectItem.aspx?&act=edit&FileType=" + modelCmsTemplateFile.FileType + "&ID=" + modelCmsTemplateFile.FileID + "&state=0\"}</script>");
                Response.End();
            }
        }
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        ManageHelper.CheckAdminPower("system_updatesubject");//权限
        modelCmsTemplateFile.FileID = PageRequest.GetInt("FileID", -1);
        if (modelCmsTemplateFile.FileID == -1)
        {
            nsb.AddErr("需修改的文件不存在。");
            nsb.ChkErr();
        }
        #region 备份修改前数据
        modelCmsTemplateFile = bllCmsTemplateFile.GetModel(modelCmsTemplateFile.FileID);

        modelCmsTemplateFileBackUp.FileID = modelCmsTemplateFile.FileID;
        modelCmsTemplateFileBackUp.FileName = modelCmsTemplateFile.FileName;
        modelCmsTemplateFileBackUp.FileContent = modelCmsTemplateFile.FileContent;
        modelCmsTemplateFileBackUp.FilePath = modelCmsTemplateFile.FilePath;
        modelCmsTemplateFileBackUp.FileType = modelCmsTemplateFile.FileType;
        modelCmsTemplateFileBackUp.FileUpdateInterval = modelCmsTemplateFile.FileUpdateInterval;
        modelCmsTemplateFileBackUp.IsTop = modelCmsTemplateFile.IsTop;
        modelCmsTemplateFileBackUp.TemplateID = modelCmsTemplateFile.TemplateID;
        modelCmsTemplateFileBackUp.UsingNum = modelCmsTemplateFile.UsingNum;
        modelCmsTemplateFileBackUp.UpdateTime = DateTime.Now;
        modelCmsTemplateFileBackUp.UserID = KeRui.BLL.Account.GetLoginUserID();
        KeRui.BLL.Account bllAccount = new KeRui.BLL.Account();
        modelCmsTemplateFileBackUp.UserName = bllAccount.GetModel(modelCmsTemplateFileBackUp.UserID).Username;
        #endregion

        modelCmsTemplateFile.FileUpdateInterval = PageRequest.GetInt("FileUpdateInterval", -1);
        if (modelCmsTemplateFile.FileUpdateInterval == -1)
        {
            nsb.AddErr("文件自动更新间隔应该为一个整数。");
        }
        modelCmsTemplateFile.FileContent = PageRequest.GetString("FileContent");
        if (modelCmsTemplateFile.FileContent == "")
        {
            nsb.AddErr("请输入文件内容。");
        }
        modelCmsTemplateFile.TemplateID = 0;
        modelCmsTemplateFile.FileType = 7;
        modelCmsTemplateFile.IsTop = PageRequest.GetInt("IsTop", 0);
        nsb.ChkErr();
        /**修改模板内容***/
        //生成文件
        KeRui.Model.CmsTemplateFile modelCmsTemplateFile1 = bllCmsTemplateFile.GetModel(modelCmsTemplateFile.FileID);
        //KeRui.BLL.Common.WriteShtmlFile(1, modelCmsTemplateFile1.FilePath, modelCmsTemplateFile.FileContent);
        FileObj.WriteFileNoSign(modelCmsTemplateFile1.FilePath, modelCmsTemplateFile.FileContent);
        /***修改数据库**/
        bllCmsTemplateFile.Update(modelCmsTemplateFile);
        bllCmsTemplateFileBackUp.Add(modelCmsTemplateFileBackUp);
        ManageHelper.AddLog("system_updatesubject" , "修改了“" + modelCmsTemplateFile.FileName + "”模板文件。");
        Response.Write("<script type=\"text/javascript\">window.parent.location.href='systemSubjectList.aspx?sucmsg=恭喜。修改成功。'</script>");
    }
    #endregion

    #region 删除
    private void Del()
    {

        ManageHelper.CheckAdminPower("system_deletesubject");//权限
        int FileID = PageRequest.GetInt("ID", -1);
        if (FileID == -1)
        {
            nsb.AddErr("该文件不存在。");
            nsb.ChkErr();
        }
        modelCmsTemplateFile = bllCmsTemplateFile.GetModel(FileID);
        if (modelCmsTemplateFile == null)
        {
            nsb.AddErr("删除的信息不存在。"); nsb.ChkErr();
        }
        modelCmsTemplateFile = bllCmsTemplateFile.GetModel(FileID);
        KeRui.BLL.Common.DelFile(modelCmsTemplateFile.FilePath, 1);
        bllCmsTemplateFile.Delete(FileID);

        ManageHelper.AddLog("system_deletesubject", "删除了“" + modelCmsTemplateFile.FileName + "”模板文件按。");
        Response.Write("<script type=\"text/javascript\">window.location.href='systemSubjectList.aspx?sucmsg=恭喜。删除成功。'</script>");
        Response.End();
    }
    #endregion

    #region 还原
    private void Reduce()
    {
        //------------------
        int FileID = PageRequest.GetInt("ID", -1);
        if (FileID == -1)
        {
            nsb.AddErr("error：传入参数有误。"); nsb.ChkErr();
        }
        modelCmsTemplateFile = bllCmsTemplateFile.GetModel(FileID);

        string FileContent = KeRui.BLL.Common.LoadTxtFile(modelCmsTemplateFile.FilePath.Replace(".shtml", "back.shtml"), 1);
        if (FileContent == "")
        {
            Response.Redirect("systemSubjectList.aspx?errmsg=还原失败。源文件不存在。");
        }
        modelCmsTemplateFile.FileContent = FileContent;
        modelCmsTemplateFile.FileID = FileID;
        bllCmsTemplateFile.Reduce(modelCmsTemplateFile);
        KeRui.BLL.Common.WriteShtmlFile(1, modelCmsTemplateFile.FilePath, FileContent);
        Response.Redirect("systemSubjectList.aspx?sucmsg=恭喜。还原成功。");
    }
    #endregion

    #region
    private void View()
    {
        int FileID = PageRequest.GetInt("ID", -1);
        modelCmsTemplateFile = bllCmsTemplateFile.GetModel(FileID);
        modelCmsTemplateFile.FileContent = modelCmsTemplateFile.FileContent;//NEED处理
        Response.Write(modelCmsTemplateFile.FileContent);
        Response.End();
    }
    #endregion

}
